﻿namespace Jsl.FxCop
{
    using Jsl.FxCop.Targets;
    using Microsoft.FxCop.Sdk;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestFixInconsistentAttributes
    {
        [TestMethod]
        public void Problems_with_attributes_on_non_components()
        {
            new string[]
            {
                "Browsable",
                "Category",
                "DefaultValue",
                "Description",
                "DesignerSerializationVisibility"
            }
            .Run(badAttribute =>
            {
                var problems = new FixInconsistentAttributes()
                    .RunFieldRule(typeof(FixInconsistentAttributes_NonComponent), "PublicFieldWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since the class isn't a component.",
                            badAttribute + "Attribute",
                            "PublicFieldWithBad" + badAttribute))
                    .HasNoMoreProblems();

                problems = new FixInconsistentAttributes()
                    .RunPropertyRule(typeof(FixInconsistentAttributes_NonComponent), "PublicPropertyWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since the class isn't a component.",
                            badAttribute + "Attribute",
                            "PublicPropertyWithBad" + badAttribute))
                    .HasNoMoreProblems();
            });
        }

        [TestMethod]
        public void Problems_with_attributes_when_Browsable_false()
        {
            new string[]
            {
                "Category",
                "Description"
            }
            .Run(badAttribute =>
            {
                var problems = new FixInconsistentAttributes()
                    .RunFieldRule(typeof(FixInconsistentAttributes_Component), "PublicFieldWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since it is Browsable(false).",
                            badAttribute + "Attribute",
                            "PublicFieldWithBad" + badAttribute))
                    .HasNoMoreProblems();

                problems = new FixInconsistentAttributes()
                    .RunPropertyRule(typeof(FixInconsistentAttributes_Component), "PublicPropertyWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since it is Browsable(false).",
                            badAttribute + "Attribute",
                            "PublicPropertyWithBad" + badAttribute))
                    .HasNoMoreProblems();
            });
        }

        [TestMethod]
        public void Problems_with_attributes_when_DesignerSerializationVisibility_hidden()
        {
            new string[]
            {
                "DefaultValue"
            }
            .Run(badAttribute =>
            {
                var problems = new FixInconsistentAttributes()
                    .RunFieldRule(typeof(FixInconsistentAttributes_Component), "PublicFieldWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since it is DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden).",
                            badAttribute + "Attribute",
                            "PublicFieldWithBad" + badAttribute))
                    .HasNoMoreProblems();

                problems = new FixInconsistentAttributes()
                    .RunPropertyRule(typeof(FixInconsistentAttributes_Component), "PublicPropertyWithBad" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since it is DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden).",
                            badAttribute + "Attribute",
                            "PublicPropertyWithBad" + badAttribute))
                    .HasNoMoreProblems();
            });
        }

        [TestMethod]
        public void Problems_with_attributes_on_non_public_fields_and_properties()
        {
            new string[]
            {
                "Browsable",
                "Category",
                "DefaultValue",
                "Description",
                "DesignerSerializationVisibility"
            }
            .Run(badAttribute =>
            {
                var problems = new FixInconsistentAttributes()
                    .RunFieldRule(typeof(FixInconsistentAttributes_Component), "NonPublicFieldWith" + badAttribute)
                    .HasProblem(
                        100,
                        FixCategories.NonBreaking,
                        MessageLevel.Warning,
                        string.Format(
                            "Remove the {0} attribute from the '{1}' member since the member is not public.",
                            badAttribute + "Attribute",
                            "NonPublicFieldWith" + badAttribute))
                    .HasNoMoreProblems();
            });
        }
    }
}